package tcu.iot.gasmanage.service;

import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tcu.iot.gasmanage.common.Result;
import tcu.iot.gasmanage.common.utils.DateUtils;
import tcu.iot.gasmanage.common.utils.GetActionDataUtils;
import tcu.iot.gasmanage.entity.Options;
import tcu.iot.gasmanage.entity.equipment.Equipment;
import tcu.iot.gasmanage.entity.equipment.EquipmentData;
import tcu.iot.gasmanage.entity.equipment.EquipmentQuery;
import tcu.iot.gasmanage.mapper.EquipmentMapper;
import tcu.iot.gasmanage.mapper.PersonMapper;
import tcu.iot.gasmanage.mapper.TDengineMapper;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class EquipmentService {

    @Resource
    private EquipmentMapper equipmentMapper;
    @Resource
    private PersonMapper personMapper;

    //获取所有设备信息
    public Result getAllEquipment(EquipmentQuery equipmentQuery) {

        UpdatePower(GetActionDataUtils.WithSignalGetActionDataLimit1(GetEquipmentWithSignal()), GetActionDataUtils.GetActionDataLimit1(GetEquipmentWithoutSignal()));

        PageHelper.startPage(equipmentQuery.getPage(), equipmentQuery.getAmount());

        HashMap<String, Object> hashMap = new HashMap<>();

        if (equipmentQuery.isSelectEquipmentNull(equipmentQuery)) {
            hashMap.put("items", equipmentMapper.SelectEquipmentByMany(equipmentQuery));
            hashMap.put("total", equipmentMapper.SelectEquipmentCount(equipmentQuery));
        } else {
            hashMap.put("items", equipmentMapper.FindAllEquipment());
            hashMap.put("total", equipmentMapper.SelectEquipmentAllCount());
        }

        return Result.ok("true", 200, "成功", hashMap);
    }

    public Result getOptions() {
        HashMap<String, Object> hashMap = new HashMap<>();

        hashMap.put("equipmentTypeOptions", equipmentMapper.FindEquipmentTypeOptions());
        hashMap.put("provinceOptions", equipmentMapper.FindProvinceOptions());
        hashMap.put("equipmentNumberOptions", equipmentMapper.FindAllEquipmentNumberName());
        hashMap.put("principalOptions", equipmentMapper.FindAllPrincipal());
        hashMap.put("regionOptions", personMapper.FindRegionOptions());
        hashMap.put("siteOptions", personMapper.FindSiteOptions());
        hashMap.put("pipelineOptions", personMapper.FindPipelineOptions());
        hashMap.put("managementOptions", personMapper.FindManagementOptions());

        return Result.ok("true", 200, "成功", hashMap);
    }

    public void UpdatePower(List<EquipmentData> signalList, List<EquipmentData> WithoutSignalList) {
//        for (EquipmentData equipmentData : list) {
//            equipmentMapper.UpdatePower(equipmentData.getPower(), equipmentData.getCsq(), equipmentData.getId());
//        }
        for (EquipmentData equipmentData : signalList) {
            equipmentMapper.UpdatePower(equipmentData.getPower(), equipmentData.getCsq(), equipmentData.getId());
        }
        for (EquipmentData equipmentData : WithoutSignalList) {
            equipmentMapper.UpdatePower(equipmentData.getPower(), -1, equipmentData.getId());
        }
    }

    public Result DeleteEquipment(int id) {
        return Result.ok("true", 200, "成功", equipmentMapper.DeleteEquipment(id));
    }

    public Result InsertEquipment(Equipment equipment) {
        if (!equipment.getEquipmentNumber().equals("当前数据库无可添加设备，请检查数据库列表")) {
            equipmentMapper.InsertEquipment(equipment.getId(), equipment.getEquipmentNumber(), equipment.getEquipmentName(), equipment.getEquipmentType(),
                    equipment.getSignalStrength(), equipment.getState(), equipment.getUploadFrequency(), equipment.getProvince(), equipment.getRegion(),
                    equipment.getSite(), equipment.getPipeline(), equipment.getManagement(), equipment.getPrincipal(), DateUtils.dealDateFormat(equipment.getAdditionTime()), DateUtils.dealDateFormat(equipment.getPatrolInspectionTime()),
                    DateUtils.dealDateFormat(equipment.getOnlineTime()), equipment.getElectricQuantity(), equipment.getRemark(), equipment.getLat(), equipment.getLng());
            return Result.ok("true", 200, "成功", "插入成功");
        } else
            return Result.failed("false", 400, "不存在可添加设备，请检查物联网数据库！");
    }

    //修改设备数据
    public Result UpdateEquipment(Equipment equipment) {
        if(equipmentMapper.UpdateEquipment(equipment.getId(), equipment.getEquipmentNumber(), equipment.getEquipmentName(), equipment.getEquipmentType(),
                equipment.getSignalStrength(), equipment.getState(), equipment.getUploadFrequency(), equipment.getProvince(), equipment.getRegion(),
                equipment.getSite(), equipment.getPipeline(), equipment.getManagement(), equipment.getPrincipal(), DateUtils.ReduceAccuracyDate(equipment.getAdditionTime()), DateUtils.ReduceAccuracyDate(equipment.getPatrolInspectionTime()),
                DateUtils.ReduceAccuracyDate(equipment.getOnlineTime()), equipment.getElectricQuantity(), equipment.getRemark(), equipment.getLat(), equipment.getLng()))
            return Result.ok("true", 200, "成功", "更新成功");
        else
            return Result.failed("false",401,"更新失败，请检查数据类型");
    }

    public Result GetTrueEquipment() {
        List<String> list = TDengineMapper.GetAllTables();

        List<String> ExistList = equipmentMapper.CheckWithTdengine();

        List<Options> OptionsList = new ArrayList<>();

        Collection<String> AllDataCollection = new ArrayList<>(list);
        Collection<String> ExistCollection = new ArrayList<>(ExistList);

        AllDataCollection.removeAll(ExistCollection);

        if (AllDataCollection.isEmpty()) {
            Options option = new Options("当前数据库无可添加设备，请检查数据库列表", "当前数据库无可添加设备，请检查数据库列表");
            OptionsList.add(option);
        } else {
            for (String s : AllDataCollection) {
                Options option = new Options(s, s);
                OptionsList.add(option);
            }
        }

        return Result.ok("true", 200, "成功", OptionsList);
    }

    public List<String> GetEquipmentWithoutSignal() {
        return equipmentMapper.GetEquipmentWithoutSignal();
    }

    public List<String> GetEquipmentWithSignal() {
        return equipmentMapper.GetEquipmentWithSignal();
    }
}
